package com.iam.core.enums;

import com.baomidou.mybatisplus.annotation.IEnum;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Arrays;
import java.util.Optional;

/** @author baily */
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public interface BaseEnum<T> extends IEnum {
    /**
     * 通过值 获取枚举对象
     * @param enumClass
     * @param value
     * @param <T>
     * @return
     */
    static <T> BaseEnum valueOfEnum(Class<BaseEnum> enumClass, T value) {
        if (value == null) {
            return null;
        }
        BaseEnum[] enums = enumClass.getEnumConstants();
        Optional<BaseEnum> optional = Arrays.asList(enums).stream().filter(baseEnum -> baseEnum.getValue().equals(value)).findAny();
        if (optional.isPresent()) {
            return optional.get();
        }
        throw new RuntimeException("未找到：" + value + "对应的" + enumClass.getName());
    }
}
